<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18
 * Time: 20:35
 */

namespace App\Process;


use EasySwoole\Config;
use EasySwoole\Core\Component\Cache\Cache;
use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Swoole\Process\AbstractProcess;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
use Swoole\Process;

class RefreshCache extends AbstractProcess
{
    protected $db;

    public function __construct(string $processName, array $args, bool $async = true)
    {
        $conf = Config::getInstance()->getConf('database');
        $this->db = Di::getInstance()->get('MYSQL');
        $this->db->setPrefix($conf['prefix']);


        parent::__construct($processName, $args, $async);
    }

    public function run(Process $process)
    {

        $this->runAll(true);
    }

    public function runAll($is_force = true)
    {
        $round_cache = Cache::getInstance()->get('round_cache');
        if (!$is_force) {
            if (empty($round_cache['round_list'])) {
                $this->refreshRoundCache();
            }
        } else {
            $this->refreshRoundCache();
        }
        $this->refreshRate();
        $this->refreshRoomList();
    }

    private function refreshRoomList()
    {
        $this->db->where('status', 1);
        $room_list = $this->db->get('game_room');
        Cache::getInstance()->set('room_list', $room_list);

    }

    private function refreshRate()
    {
        $where = ['da_rate', 'xiao_rate', 'dan_rate', 'shuang_rate', 'two_star_rate', 'return_rate'];
        $rate = [];
        $this->db->where('name', ['in' => $where]);
        $res = $this->db->get('settings', null, ['name', 'value']);

        $return_rate = [];
        if (!empty($res)) {
            foreach ($res as $item => $v) {
                if ($v['name'] == 'return_rate') {
                    $return_rate['return_rate'] = $v['value'];
                    continue;
                }
                $rate[$v['name']] = $v['value'];


            }
        }

        Cache::getInstance()->set('game_rate', $rate);
        Cache::getInstance()->set('return_rate', $return_rate);
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    /*private function refreshRoundCache()
    {
        $this->db->orderBy('round_id', 'asc');
        $round_list = $this->db->get('round');
        $round_cache = [];
        //当天零点时间戳
        $current_time_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $current_time_end = mktime(11, 59, 59, date('m'), date('d'), date('Y'));
        $str_n = '';

        $temp_id = 0;
        foreach ($round_list as $item => $value) {

            if (strtotime($value['round_stop_time'],$current_time_start) < strtotime($value['round_start_time'],$current_time_start)) {
                $temp_id = $value['round_id'];
                $round_cache[] = [
                    'round_id' => $value['round_id'],
                    'start_time' => strtotime($value['round_start_time'],$current_time_start),
                    'start_date' => date('Y-m-d H:i:s', strtotime($value['round_start_time'],$current_time_start)),
                    'stop_time' => strtotime('+1 day ' . $value['round_stop_time'],$current_time_start),
                    'stop_date' => date('Y-m-d H:i:s', strtotime('+1 day ' . $value['round_stop_time'],$current_time_start)),
                    'open_time' => strtotime('+1 day ' . $value['round_open_time'],$current_time_start),
                    'open_date' => date('Y-m-d H:i:s', strtotime('+1 day ' . $value['round_open_time'],$current_time_start)),
                    'round_circle' => $value['round_circle'],
                ];


            } else {
                if ($temp_id != 0 && ($value['round_id'] > $temp_id)) {

                    if (strtotime($value['round_start_time'],$current_time_start) >= $current_time_start) {
                        $str_n = '+1 day ';
                    }
                    $round_cache[] = [
                        'round_id' => $value['round_id'],
                        'start_time' => strtotime($str_n . $value['round_start_time'],$current_time_start),
                        'start_date' => date('Y-m-d H:i:s', strtotime($str_n . $value['round_start_time'],$current_time_start)),
                        'stop_time' => strtotime('+1 day ' . $value['round_stop_time'],$current_time_start),
                        'stop_date' => date('Y-m-d H:i:s', strtotime('+1 day ' . $value['round_stop_time'],$current_time_start)),
                        'open_time' => strtotime($str_n . $value['round_open_time'],$current_time_start),
                        'open_date' => date('Y-m-d H:i:s', strtotime($str_n . $value['round_open_time'],$current_time_start)),
                        'round_circle' => $value['round_circle'],
                    ];
//                }
                } else {
                    $round_cache[] = [
                        'round_id' => $value['round_id'],
                        'start_time' => strtotime($value['round_start_time'],$current_time_start),
                        'start_date' => date('Y-m-d H:i:s', strtotime($value['round_start_time'],$current_time_start)),
                        'stop_time' => strtotime($value['round_stop_time'],$current_time_start),
                        'stop_date' => date('Y-m-d H:i:s', strtotime($value['round_stop_time'],$current_time_start)),
                        'open_time' => strtotime($value['round_open_time'],$current_time_start),
                        'open_date' => date('Y-m-d H:i:s', strtotime($value['round_open_time'],$current_time_start)),
                        'round_circle' => $value['round_circle'],
                    ];

                }
            }

        }

        print_r($round_cache);
        $round_cache = [
            'game_period' => date('Ymd',$current_time_start),
            'round_list' => $round_cache
        ];

        Cache::getInstance()->set('round_cache', $round_cache);

    }*/

    private function refreshRoundCache()
    {
        $this->db->orderBy('round_id', 'asc');
        $round_list = $this->db->get('round');
        $round_cache = [];
        //当天零点时间戳
        $current_time_start = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

        $str_n = "";
        foreach ($round_list as $item => $value) {
            if (strtotime($value['round_stop_time']) < strtotime($value['round_start_time'])) {
                $str_n = "+1 day ";
            }
            $round_cache[] = [
                'round_id' => $value['round_id'],
                'start_time' => strtotime($value['round_start_time'], $current_time_start),
                'start_date' => date('Y-m-d H:i:s', strtotime($value['round_start_time'], $current_time_start)),
                'stop_time' => strtotime($str_n . $value['round_stop_time'], $current_time_start),
                'stop_date' => date('Y-m-d H:i:s', strtotime($str_n . $value['round_stop_time'], $current_time_start)),
                'open_time' => strtotime($str_n . $value['round_open_time'], $current_time_start),
                'open_date' => date('Y-m-d H:i:s', strtotime($str_n . $value['round_open_time'], $current_time_start)),
                'round_circle' => $value['round_circle'],
            ];

        }

        $round_cache = [
            'game_period' => date('Ymd', $current_time_start),
            'round_list' => $round_cache
        ];
        Cache::getInstance()->set('round_cache', $round_cache);
    }

    public function onReceive(string $str, ...$args)
    {
        switch ($str) {
            case 'refresh':
                $this->runAll(true);
                break;
            case 'refreshRate':
                $this->refreshRate();
                break;
            case 'refreshRoom':
                $this->refreshRoomList();
                break;

        }
    }

}